import router from './router'
import store from '@/store'
const whiteList = ['/login', '/404'] // no redirect whitelist
import NProgress from 'nprogress'
// 导入进度条样式
import 'nprogress/nprogress.css'
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const token = store.getters.token
  if (token) {
    if (!store.getters.name) {
      store.dispatch('user/getUserInfoActions')
    }
    if (to.path === '/login') {
      next(from.path)
    } else {
      next()
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      if (whiteList.includes(to.path)) {
        next()
      } else {
        next('/login')
      }
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
